home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2000 September / september_2000.iso / intercd / root / ^Linux / cfengine-1.5.3 / src / rotate.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-05-28  |  3.4 KB  |  120 lines

  1. /* cfengine for GNU
  2.  
  3.         Copyright (C) 1995
  4.         Free Software Foundation, Inc.
  5.  
  6.    This file is part of GNU cfengine - written and maintained 
  7.    by Mark Burgess, Dept of Computing and Engineering, Oslo College,
  8.    Dept. of Theoretical physics, University of Oslo
  9.  
  10.    This program is free software; you can redistribute it and/or modify it
  11.    under the terms of the GNU General Public License as published by the
  12.    Free Software Foundation; either version 2, or (at your option) any
  13.    later version.
  14.  
  15.    This program is distributed in the hope that it will be useful,
  16.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.    GNU General Public License for more details.
  19.  
  20.   You should have received a copy of the GNU General Public License
  21.   along with this program; if not, write to the Free Software
  22.   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
  23.  
  24. */
  25.  
  26.  
  27. /*********************************************************************/
  28. /*                                                                   */
  29. /*  TOOLKITS: "rotate" library                                       */
  30. /*                                                                   */
  31. /*********************************************************************/
  32.  
  33. #include "cf.defs.h"
  34. #include "cf.extern.h"
  35. #include "../pub/global.h"
  36. #include "../pub/md5.h"
  37.  
  38.  
  39. /*********************************************************************/
  40.  
  41. RotateFiles(name,number)
  42.  
  43.  /* Rotates file extensions like in free bsd, messages, syslog */
  44.  /* etc. Note that this doesn't check for failure, since the   */
  45.  /* aim is to disable the files anyway                         */
  46.  
  47.  
  48. char *name;
  49. int number;
  50.  
  51. { int i, fd;
  52.   struct Image dummy;
  53.   struct stat statbuf;
  54.  
  55. if (stat(name,&statbuf) == -1)
  56.    {
  57.    Verbose("No access to file %s\n",name);
  58.    return;
  59.    }
  60.  
  61. for (i = number-1; i > 0; i--)
  62.    {
  63.    if (BufferOverflow(name,"1"))
  64.       {
  65.       CfLog(cferror,"Culprit: RotateFiles in Disable:\n","");
  66.       return;
  67.       }
  68.    
  69.    sprintf(CURRENTITEM,"%s.%d",name,i);
  70.    sprintf(VBUFF,"%s.%d",name, i+1);
  71.    
  72.    if (rename(CURRENTITEM,VBUFF) == -1)
  73.       {
  74.       Debug("Rename failed in RotateFiles %s -> %s\n",name,CURRENTITEM);
  75.       }
  76.  
  77.    sprintf(CURRENTITEM,"%s.%d.gz",name,i);
  78.    sprintf(VBUFF,"%s.%d.gz",name, i+1);
  79.    
  80.    if (rename(CURRENTITEM,VBUFF) == -1)
  81.       {
  82.       Debug("Rename failed in RotateFiles %s -> %s\n",name,CURRENTITEM);
  83.       }
  84.  
  85.    sprintf(CURRENTITEM,"%s.%d.Z",name,i);
  86.    sprintf(VBUFF,"%s.%d.Z",name, i+1);
  87.    
  88.    if (rename(CURRENTITEM,VBUFF) == -1)
  89.       {
  90.       Debug("Rename failed in RotateFiles %s -> %s\n",name,CURRENTITEM);
  91.       }   
  92.    }
  93.  
  94. sprintf(VBUFF,"%s.1",name);
  95.  
  96. dummy.server = "localhost";
  97. dummy.cache = NULL;
  98. dummy.inode_cache = NULL;
  99.  
  100. if (CopyRegDisk(name,VBUFF,&dummy) == -1)
  101.    {
  102.    Debug2("cfengine: copy failed in RotateFiles %s -> %s\n",name,VBUFF);
  103.    }
  104.  
  105. chmod(VBUFF,statbuf.st_mode);
  106. chown(VBUFF,statbuf.st_uid,statbuf.st_gid); 
  107.  
  108. if ((fd = creat(name,statbuf.st_mode)) == -1)
  109.    {
  110.    sprintf(OUTPUT,"Failed to create new %s in disable(rotate)\n",name);
  111.    CfLog(cferror,OUTPUT,"creat");
  112.    }
  113. else
  114.    {
  115.    chown(name,statbuf.st_uid,statbuf.st_gid); /* NT doesn't have fchown */
  116.    fchmod(fd,statbuf.st_mode);
  117.    close(fd);
  118.    }
  119. }
  120.